//
//  InputViewController.m
//  DPGJ
//
//  Created by ZhuoLaiQiang@Gmail.com on 11-11-29.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "InputViewController.h"
#import <QuartzCore/QuartzCore.h>

@interface InputViewController() 

- (void)performWithSelector:(SEL)selector withObject:(id)p1 withObject:(id)p2 tager:(id)object_;


@end

@implementation InputViewController

@synthesize sign                            =                           _sign;
@synthesize mainView                        =                           _mainView;
@synthesize inputTextFile                   =                           _inputTextFile;
@synthesize titlelabel                      =                           _titlelabel;
@synthesize delegate                        =                           _delegate;
@synthesize text                            =                           _text;
@synthesize finishInputSel                  =                           _finishInputSel;

- (void)dealloc
{
    LF_RELEASE_SAFELY(_sign);
    LF_RELEASE_SAFELY(_text);
    
    [_mainView release];
    [_inputTextFile release];
    [_titlelabel release];
    _finishInputSel = nil;
    
    [super dealloc];
}

- (id)initWithDelegate:(id)delegate_ sign:(id)sign_ titleLabel:(NSString *)text_ finishInputSel:(SEL)finishInputSel_
{
    self = [super init];
    if (self)
    {
        self.delegate = delegate_;
        self.sign = sign_;
        self.text = text_;
        self.finishInputSel = finishInputSel_;
    }
    
    return self;
}

- (IBAction)fixButtonIsPressed:(id)sender 
{
    [UIView beginAnimations: nil context: nil];
    [UIView setAnimationDuration: 0.3];
    [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDelegate: self];
    [UIView setAnimationDidStopSelector: @selector(animationIsEnd)];
    
    
    self.navigationController.view.frame = CGRectMake(0, 
                                                      -self.navigationController.view.frame.size.height, 
                                                      320, 
                                                      self.navigationController.view.frame.size.height);
    [_inputTextFile resignFirstResponder];
    
    [UIView commitAnimations];
}

- (void)animationIsEnd
{
    if (_delegate && [_delegate respondsToSelector: _finishInputSel])
    {
        [self performWithSelector: _finishInputSel withObject: _inputTextFile.text withObject: _sign tager: _delegate];
    }
}

- (void)performWithSelector:(SEL)selector withObject:(id)p1 withObject:(id)p2 tager:(id)object_

{
    NSMethodSignature *sig = [object_ methodSignatureForSelector:selector];

    if (sig) 
        
    {
        NSInvocation* invo = [NSInvocation invocationWithMethodSignature:sig];
        
        [invo setTarget: object_];
        
        [invo setSelector:selector];
        
        [invo setArgument:&p1 atIndex:2];
        [invo setArgument:&p2 atIndex:3];
    
        [invo invoke];
    } 
}

- (void)cancel
{
    [UIView beginAnimations: nil context: nil];
    [UIView setAnimationDuration: 0.3];
    [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDelegate: self];
    
    
    self.navigationController.view.frame = CGRectMake(0, 
                                                      -self.navigationController.view.frame.size.height, 
                                                      320, 
                                                      self.navigationController.view.frame.size.height);
    [_inputTextFile resignFirstResponder];
    
    [UIView commitAnimations];
}

#pragma mark - View lifecycle
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle: @"取消" 
                                                                             style: UIBarButtonItemStylePlain 
                                                                            target: self 
                                                                            action: @selector(cancel)] autorelease];
    
    if (self.text)
    {
        _titlelabel.text = self.text;
    }
    
    [_inputTextFile becomeFirstResponder];
    
    _mainView.layer.shadowColor = [UIColor colorWithRed: 0 green:0 blue:0 alpha:1].CGColor;
    _mainView.layer.shadowOffset = CGSizeMake(0, 20);
    _mainView.layer.shadowOpacity = 0.3;
}

- (void)viewDidUnload
{
    [self setMainView:nil];
    [self setInputTextFile:nil];
    [self setTitlelabel:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}



@end
